package com.droidblackbox.manager;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.droidblackbox.R;

public final class NotiManager {

	private Context context = null;

	private NotificationManager notificationManager = null;
	private Notification notification = null;

	private String appName;
	private int id = -1;
	private String ticker = "";
	private String text = "";
	private int flags = -1;

	private Intent intent;

	public NotiManager(Context context) {
		this.context = context;
		this.appName = context.getResources().getString(R.string.app_name);
		notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);

	}

	public void setId(int id) {
		this.id = id;
	}

	public void setTicker(String ticker) {
		this.ticker = ticker;
	}

	public void setText(String text) {
		this.text = text;
	}

	public void setFlags(int flags) {
		this.flags = flags;
	}

	public void setIntent(Intent intent) {
		this.intent = intent;
	}

	public void onNotification() {
		do {
			if (id < 0)
				break;

			if (ticker.equals(""))
				break;

			if (text.equals(""))
				break;

			if (flags < 0)
				break;

			if (intent == null)
				break;
			
			notification();

		} while (false);
	}

	public void cancel(int id) {

		if (id <= 0)
			notificationManager.cancelAll();
		else
			notificationManager.cancel(id);
	}

	private void notification() {

		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);

		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
				intent, 0);

		notification = new Notification(R.drawable.ic_launcher, ticker,
				System.currentTimeMillis());
		notification.flags |= flags;
		notification.setLatestEventInfo(context, appName, text, pendingIntent);

		notificationManager.notify(id, notification);
	}
}
